﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Xml.Linq;
using ThreeByte.ActiveDeck.Controls;
using ThreeByte.Media;
using ThreeByte.ActiveDeck.Config;
using System.Windows.Data;
using System.Windows;
using ThreeByte.Converters;

namespace ThreeByte.ActiveDeck.AddIn.Content.Controls
{
    public class DesignerImage : DesignerControl
    {
        public GraphicResource Image {
            get;
            private set;
        }

        private bool _fill;
        public bool Fill {
            get {
                return _fill;
            }
            set {
                _fill = value;
                NotifyPropertyChanged(@"Fill");
            }
        }

        private VerticalAlignment _verticalAlignment = VerticalAlignment.Center;
        public VerticalAlignment VerticalAlignment {
            get {
                return _verticalAlignment;
            }
            set {
                _verticalAlignment = value;
                NotifyPropertyChanged(@"VerticalAlignment");
            }
        }

        private HorizontalAlignment _horizontalAlignment = HorizontalAlignment.Center;
        public HorizontalAlignment HorizontalAlignment {
            get {
                return _horizontalAlignment;
            }
            set {
                _horizontalAlignment = value;
                NotifyPropertyChanged(@"HorizontalAlignment");
            }
        }

        public DesignerImage(GraphicResource image, PixelSpace pos)
            : base(pos) {
                Image = image;
        }

        public DesignerImage(XElement node, Dictionary<Guid, GraphicResource> resources) {

            //Get the media path
            XElement mediaNode = node.Element(ns + "Image");

            Guid resId = Guid.Parse(mediaNode.Attribute("ResourceID").Value);
            Image = resources[resId];

            //Get the Position/Size
            int top = int.Parse(node.Attribute("Top").Value);
            int left = int.Parse(node.Attribute("Left").Value);
            int width = int.Parse(node.Attribute("Width").Value);
            int height = int.Parse(node.Attribute("Height").Value);

            if(mediaNode.Attribute("ScaleToFill") != null) {
                Fill = bool.Parse(mediaNode.Attribute("ScaleToFill").Value);
            }

            if(mediaNode.Attribute("VerticalAlignment") != null) {
                VerticalAlignment = (VerticalAlignment)Enum.Parse(typeof(VerticalAlignment), mediaNode.Attribute("VerticalAlignment").Value);
            }
            if(mediaNode.Attribute("HorizontalAlignment") != null) {
                HorizontalAlignment = (HorizontalAlignment)Enum.Parse(typeof(HorizontalAlignment), mediaNode.Attribute("HorizontalAlignment").Value);
            }

            //Update this object
            DefaultPixelSpace = new PixelSpace() { W = 768, H = 1024, X = 0, Y = 0 };
            CurrentPixelSpace = new PixelSpace() { X = left, Y = top, W = width, H = height };
        }

        public override XElement ToXml() {
            XElement node = base.ToXml();
            node.Add(PixelSpaceAsAttributes(CurrentPixelSpace));
            XElement mediaNode = new XElement(ns + "Image");
            mediaNode.Add(new XAttribute("ResourceID", Image.ID));
            mediaNode.Add(new XAttribute("ScaleToFill", Fill));
            mediaNode.Add(new XAttribute("VerticalAlignment", VerticalAlignment));
            mediaNode.Add(new XAttribute("HorizontalAlignment", HorizontalAlignment));
            node.Add(mediaNode);
            return node;
        }

        public override XElement GetPresentationXml(PixelTranslator translation = null) {
            XElement node = new XElement(ns + "Image");
            node.Add(new XAttribute("ResourceID", Image.ID));
            PixelSpace pos = CurrentPixelSpace;
            if(translation != null) {
                pos = translation.Translate(pos);
            }
            node.Add(PixelSpaceAsAttributes(pos));
            if(!string.IsNullOrWhiteSpace(Condition)) {
                node.Add(new XAttribute("Condition", Condition));
            }

            return node;
        }

        public override List<GraphicResource> GetGraphicResources() {
            List<GraphicResource> resources = base.GetGraphicResources();
            resources.Add(Image);
            return resources;
        }
        
    }
}
